package com.tqw.client.service.impl;

import com.tqw.client.service.ClientFeignService;
import com.tqw.client.vo.ReceivingVO;
import com.tqw.entity.Result;
import com.tqw.pojo.Area;
import com.tqw.shipping.feign.AreaFeignClient;
import com.tqw.shipping.feign.ShippingDTO;
import com.tqw.shipping.feign.ShippingFeignClient;
import com.tqw.vo.ShippingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestHeader;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Will
 */
@Service
public class ClientFeignServiceImpl implements ClientFeignService {

    @Autowired
    private AreaFeignClient areaServer;

    /**
     * 从tqw-shipping-api的feign接口中查询地址列表Result<List<ShippingVO>>
     *     得到其中的 ShippingVO 封装成 ReceivingVO 对象
     *     返回 List<ReceivingVO> 对象
     * @param
     * @return
     */
    @Override
    public List<ReceivingVO> shippingAddressList(String token) {
        Result<List<ShippingDTO>> resultShippingVOList = areaServer.list(token);
        List<ShippingDTO> dataList = resultShippingVOList.getData();
        List<ReceivingVO> receivingVOList = new ArrayList<>();
        for (ShippingDTO shippingDTO : dataList) {
            receivingVOList.add(new ReceivingVO(shippingDTO));
        }
        return receivingVOList;
    }

    /**
     * 个人中心地址 编辑对话框数据回显
     * @param
     * @param
     * @return
     */
    @Override
    public ReceivingVO shippingAddressOne(String token,Integer addressId) {
        Result<ShippingVO> infoById = areaServer.getAddrId(token,addressId);
        return new ReceivingVO(infoById.getData());
    }

    /**
     * 联查地区
     * @return
     */
    @Override
    public List<Area> shippingArea() {
        Result<List<Area>> allArea = areaServer.getAllArea();
        return allArea.getData();
    }

    /**
     * 更新收货地址
     * @param shippingVO
     * @return
     */
    @Override
    public Result shippingReceivingUpdate(String token,ShippingVO shippingVO) {
        Result update = areaServer.update(token,shippingVO);
        return update;
    }

    /**
     * 删除收货地址
     * @param addressId
     * @return
     */
    @Override
    public Result shippingReceivingDelete(String token,Integer addressId) {
        Result delete = areaServer.delete(token,addressId);
        return delete;
    }

    /**
     * 新增收货地址
     * @param shippingVO
     * @return
     */
    @Override
    public Result shippingReceivingInsert(@RequestHeader("Authorization") String token, ShippingVO shippingVO) {
        Result save = areaServer.save(token,shippingVO);
        return save;
    }


}
